package icursoft.educationms;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentActivity;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;
import android.widget.Toast;

import com.readystatesoftware.systembartint.SystemBarTintManager;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import icursoft.educationms.eventbus.EventError;
import icursoft.educationms.eventbus.EventExit;
import icursoft.educationms.servive.PreferencesService;
import icursoft.educationms.util.MachTalkUtil;

/**
 * Created by wang on 2017/4/19.
 */

public abstract class BaseActivity extends FragmentActivity implements View.OnClickListener {
    protected final String TAG = getClass().getSimpleName();
    protected Context mContext;
    private SystemBarTintManager tintManager;
    private Dialog mLoadingDialog;
    private String msg;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        initWindow();
        // 取消标题栏
        int layoutId = getLayoutId();
        if (layoutId != 0) {
            setContentView(layoutId);
            getWindow().setBackgroundDrawable(null);
            mContext = getApplicationContext();

            Log.i(TAG, "InticareDApplication.getInstance()==null:" + (EducationMSApplication.getInstance() == null));
            EducationMSApplication.getInstance().addActivity(this);
            preliminary();
        } else {
            return;
        }
    }

//    @Override
//    public void onCreate(Bundle savedInstanceState, PersistableBundle persistentState) {
//        super.onCreate(savedInstanceState, persistentState);
//
//
//    }

    private void initWindow() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
            tintManager = new SystemBarTintManager(this);
            tintManager.setStatusBarTintColor(getResources().getColor(R.color.bg_action_bar));
            tintManager.setStatusBarTintEnabled(true);
        }
    }

    @Override
    protected void onDestroy() {
        EventBus.getDefault().unregister(this);
        super.onDestroy();
    }

    protected void preliminary() {
        setupViews();
        EventBus.getDefault().register(this);
        initialized();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void HttpError(EventError error) {
        if (error != null) {

        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void Exit(EventExit exit) {
        if (exit.isExit()) {
            finish();
        }
    }

    public Context getContext() {
        return mContext;
    }


    protected abstract int getLayoutId();


    protected abstract void setupViews();


    protected abstract void initialized();


    @SuppressWarnings("unchecked")
    public <T extends View> T getView(int rid) {
        return (T) findViewById(rid);
    }

    public void startActivity(Class<?> cls) {
        startActivity(cls, null);
    }


    public void startActivity(Class<?> cls, Bundle bundle) {
        Intent intent = new Intent();
        intent.setClass(this, cls);
        if (bundle != null) {
            intent.putExtras(bundle);
        }
        startActivity(intent);
        overridePendingTransition(R.anim.push_left_in, R.anim.push_left_out);
    }

    @Override
    public void finish() {
        super.finish();
        Log.i(TAG, "Finish()");
        EducationMSApplication.getInstance().deleteActivity(this);
        overridePendingTransition(R.anim.push_right_in, R.anim.push_right_out);
    }

    private Dialog showLoading(Context context, String text) {
        Dialog dialog = null;
        dialog = new Dialog(context, R.style.CommonLoadingShadeDialog);
        View view = LayoutInflater.from(context).inflate(R.layout.layout_progress, null);
        TextView loadingtext = (TextView) view.findViewById(R.id.loading_text);
        if (!TextUtils.isEmpty(text))
            loadingtext.setText(text);
        dialog.setContentView(view);
        dialog.show();
        return dialog;
    }

    public void showLoadingDialog() {
        Log.i("LoadingDialog", "==showLoadingDialog==");
        mLoadingDialog = showLoading(this, getResources().getString(R.string.xlistview_header_hint_loading));
    }

    public void showLoadingDialog(int text) {
        mLoadingDialog = showLoading(this, getResources().getString(text));
    }

    public void dismissLoadingDialog() {
        Log.i("LoadingDialog", "==dismissLoadingDialog==");
        if (mLoadingDialog != null) {
            mLoadingDialog.dismiss();
            mLoadingDialog = null;
        }
    }
}
